<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>ZDH 用户维度关系首页</title>

    <meta name="keywords" content="ZDH 用户维度关系首页">
    <meta name="description" content="ZDH 用户维度关系首页">

    <link rel="shortcut icon" href="img/favicon.ico">
    <link href="css/plugins/chosen/chosen.css" rel="stylesheet">
    <link href="css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="css/plugins/jsTree/style.min.css" rel="stylesheet">
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css?v=4.1.0" rel="stylesheet">

</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content  animated fadeInRight">

        <div class="row">
            <div class="col-sm-6">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>维度列表</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="dropdown-toggle" data-toggle="dropdown" href="buttons.html#">
                                <i class="fa fa-eye"></i>
                            </a>
                            <ul class="dropdown-menu dropdown-user">
                                <li><a href="javascript:void(0);" onclick="getResourceDesc()">功能说明</a>
                                </li>
                            </ul>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">

                        <form role="form" class="form-inline" id="data_sources_form">
                            <div class="form-group">
                                <input id="search1" onclick="get_auth()"  οnsubmit='return false' type="button" class="btn btn-primary form-control" style="margin-bottom:0px" value="获取绑定"/>
                                <input id="search2" onclick="update_auth()"  οnsubmit='return false' type="button" class="btn btn-primary form-control" style="margin-bottom:0px" value="更新"/>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group" style="width: 100%">
                            <select id="dim_code" name="dim_code"
                                    data-placeholder="维度code"
                                    class="chosen-select form-control m-b" tabindex="2" style="width: 35%">
                                <option value="">空</option>
                            </select>
                            </div>
                            <div class="form-group" style="width: 100%;"></div>

                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <div> <input id="search_input" name="search_input" class="form-control" type="text" value="" placeholder="资源信息"></div><br />
                                <div id="using_json3"></div>
                            </div>

                        </form>


                    </div>
                </div>
            </div>

        </div>


    </div>


    <script src="js/zdh_common.js"></script>
    <!-- 全局js -->
    <script src="js/jquery.min.js?v=2.1.4"></script>
    <script src="js/bootstrap.min.js?v=3.3.6"></script>



    <!-- 自定义js -->
    <script src="js/content.js?v=1.0.0"></script>
    <!-- Chosen -->
    <script src="js/plugins/chosen/chosen.jquery.js"></script>

    <!-- jsTree plugin javascript -->
    <script src="js/plugins/layer/layer.min.js"></script>
    <script src="js/plugins/jsTree/jstree.min.js"></script>

    <style>
        .jstree-open > .jstree-anchor > .fa-folder:before {
            content: "\f07c";
        }

        .jstree-default .jstree-icon.none {
            width: 0;
        }
    </style>

    <script>

        function init_jstree(product_code, dim_code){
            $('#using_json3').jstree("destroy");
            $('#using_json3').jstree({
                "plugins" : [ "types","checkbox", "search"],
                'checkbox': { cascade: "", three_state: false }, //级联
                'core' : {
                    "check_callback": true,
                    'data' :  function (obj, callback){
                        $.ajax({
                            type : "post",
                            url : server_context+"/permission_dimension_value_node",//getAreaBuild=getTreeArea
                            data: {"product_code": product_code, "dim_code": dim_code},
                            dataType : "json",
                            async:'false',
                            success : function(data) {
                                console.info(data);
                                if(data.code == "200") {
                                    callback.call(this, data.result);
                                }else{
                                    callback.call(this, []);
                                    layer.msg(data.msg);
                                }
                            }
                        });
                    }


                } });
            $('#search_input').keyup(function () {
                $('#using_json3').jstree(true).search($('#search_input').val());

            });
        }

        var url = location.search; //这一条语句获取了包括问号开始到参数的最后，不包括前面的路径
        var params = url.substr(1);//去掉问号
        var pa = params.split("&");
        var s = new Object();
        for(var i = 0; i < pa.length; i ++){
            s[pa[i].split("=")[0]] = unescape(pa[i].split("=")[1]);
        }

        var product_code = s.product_code;
        var user_account = s.user_account;
        var dim_code = s.dim_code;

        //初始化维度下拉框
        $.ajax({
            type: 'POST',
            url: server_context+"/permission_dimension_list_by_product_code",
            async:false,
            dataType: 'json',
            data: {"product_code": product_code},
            //成功返回
            success: function (data) {
                if(data.code != "200"){
                    layer.msg(data.msg);
                    return ;
                }
                var str = '<option value=\"\" hassubinfo=\"true\">空</option>';
                for (var i = 0; i < data.result.length; i++) {
                    str += '<option value=\"' + data.result[i].dim_code + '\" hassubinfo=\"true\">' + data.result[i].dim_name + '</option>';
                }
                $('#dim_code').html(str);
                $("#dim_code").trigger("chosen:updated");
                $('#dim_code').chosen().on("change", function (evt, params) {
                    init_jstree(product_code, $('#dim_code').val());
                });
            },
            //处理完成
            complete: function () {
            },
            //报错
            error: function (data) {
            }
        });


        if(s.id != '-1'){
            $("#dim_code").val(dim_code);
            $("#dim_code").attr("disenabled", "disenabled");
            $("#dim_code").trigger("chosen:updated");
            init_jstree(product_code, dim_code);
        }

        function get_auth(){
            $.ajax({
                type : "POST",
                url : server_context+"/permission_user_dimension_value_list",
                dataType : "json",
                data : {"product_code":product_code, "dim_code": $("#dim_code").val(), "user_account": user_account},
                async:'false',
                success : function(data) {
                    if(data.code != "200"){
                        layer.msg(data.msg);
                        return ;
                    }
                    for(var i=0;i<data.result.length;i++){
                        $('#using_json3').jstree('check_node',data.result[i].dim_value_code);
                    }
                    $('#using_json3').jstree('open_all')
                }
            });
        }

        function update_auth() {


            var url = location.search; //这一条语句获取了包括问号开始到参数的最后，不包括前面的路径
            var params = url.substr(1);//去掉问号
            var pa = params.split("&");
            var s = new Object();
            for(var i = 0; i < pa.length; i ++){
                s[pa[i].split("=")[0]] = unescape(pa[i].split("=")[1]);
            }

            if($('#dim_code').val() == ''){
                layer.msg("维度code不可为空")
            }

            var ids = $('#using_json3').jstree("get_checked");

            if(ids.length < 1){
                layer.msg("未选择权限,无法更新");
                return ;
            }

            var level = parseInt(s.level)+1;

            var index1 = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });

            //用户id->功能id
            $.ajax({
                type : "POST",
                url : server_context+"/permission_user_dimension_value_update",
                dataType : "json",
                data : {
                    dim_code:$('#dim_code').val(),
                    dim_value_codes:ids,
                    level:level,
                    user_account: user_account,
                    product_code: product_code
                },
                async:'false',
                traditional: true,
                success : function(data) {
                    console.info(data);
                    if(data.code != '200'){
                        console.error(data.msg);
                        layer.msg("执行失败");
                        return ;
                    }
                    layer.msg("完成更新");
                    layer.close(index1)
                },
                //处理完成
                complete: function () {
                    layer.close(index1);
                    console.info("complete")
                },
            });


        }

    </script>

</body>

</html>
